import HelperAxios from 'axios'
import { ElMessage } from 'element-plus'
// import { getCurrentInstance } from 'vue'
// const { proxy } = getCurrentInstance()
// const token = window.localStorage.getItem('token')
// console.log(token);
const axiosInstance = HelperAxios.create({
    baseURL: 'http://api_devs.wanxikeji.cn',
    // headers['token']:token
})

function reqFuncAxios(methodIn, urlIn, paramsIn, ...callBackFunIn) { // 请求方法 请求地址 请求参数
    let reqParam = { url: urlIn, method: methodIn }
    switch (methodIn) {
        case 'post': case 'put': reqParam.data = paramsIn; break
        case 'get': case 'delete': reqParam.params = paramsIn; break
    }
    // reqParam.token = token
    const promiseReq = axiosInstance(reqParam) // 开始请求
    promiseReq
        .then((res) => {
            if (res.status === 200) { // 接口响应成功 请求成功
                console.log('接口响应成功');
                if (res.data.code === 3000) { // token失效
                    ElMessage.warning(res.data.msg);
                    // proxy.$router.push({ path: '/login' })
                    window.localStorage.clear(); window.sessionStorage.clear()
                    window.location.pathname = '/login'
                } else {
                    if (callBackFunIn.length > 0) { callBackFunIn[0](res) } // 这样第四个参数为可选 为函数 若有则执行 若无则下一步 参数为请求结果
                }
                // else { console.log('no callBackFunIn'); }
            }
            else { console.log('接口响应失败', res); }
            return res
        })
        .catch(res => { console.log('请求失败', res); return res })
    return promiseReq
}
export default reqFuncAxios